package roman.mosiienko.yalantistestrm.fragments;

import java.io.IOException;
import java.net.URL;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

import roman.mosiienko.yalantistestrm.MainActivity;
import roman.mosiienko.yalantistestrm.R;
import roman.mosiienko.yalantistestrm.mymodels.Details;
import android.app.Fragment;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

public class MainFragment extends Fragment {
	public static String getID = "1";
	public static int Number = 1;
	private static ImageView picture;
	private static TextView title;
	private static TextView body;
	private static TextView content;
	private  View rootView;
	private MainActivity ma;
	public MainFragment(MainActivity ma) {
	
		this.ma = ma;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		 rootView = inflater.inflate(R.layout.fragment_main, container,
				false);
		title = (TextView) rootView.findViewById(R.id.textViewRightWindowTitle);
		body = (TextView) rootView.findViewById(R.id.textViewRightWindowBody);
		content = (TextView) rootView.findViewById(R.id.TextViewRightWindowContent);
		picture = (ImageView) rootView.findViewById(R.id.imageViewRightWindow);
		
		ConnectivityManager cm = (ConnectivityManager)ma.getSystemService(Context.CONNECTIVITY_SERVICE);
	    NetworkInfo nInfo = cm.getActiveNetworkInfo();
	    if (nInfo != null && nInfo.isConnected()) {
	        
	        new DownloadContent().execute();
	    }
	    else {
	    	
	    	Toast.makeText(ma, "Network error", Toast.LENGTH_SHORT).show();
	    }
		return rootView;
	}

	private class DownloadContent extends
			AsyncTask<String, Void, Details> {
		protected Details doInBackground(String... urls) {
			ObjectMapper mapper = new ObjectMapper();
			String detailsBaseAddr = "http://apitest.yalantis.com/test/?id="
					+ (MainFragment.Number + 1);
			try {
				JsonNode rootDetails = mapper.readTree(new URL(detailsBaseAddr)
						.openStream());
				JsonNode detailsNode = rootDetails.findValue("data");
				Details detail = mapper.readValue(detailsNode, Details.class);
				return detail;
			} catch (JsonGenerationException e) {
				e.printStackTrace();
			} catch (JsonMappingException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return null;
		}

		protected void onPostExecute(Details detail) {
			title.setText(detail.getTitle());
			body.setText(detail.getBody());
			content.setText(detail.getContent());
		
			ImageLoader imageLoader = ImageLoader.getInstance();
			imageLoader.init(ImageLoaderConfiguration.createDefault(rootView.getContext()));
			imageLoader.displayImage(detail.getPicture(),picture);
			getActivity().setTitle(title.getText());
			
		}
	}
}